﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Mail;
using System.Threading;
using ChinaDynastyTours.Controls;
using ChinaDynastyTours.Models;
using System.Net.Mime;

namespace ChinaDynastyTours.Helpers
{
    class EMailHelper
    {
       /// <summary>
       /// 
       /// </summary>
       /// <param name="mailtoAddress">mail地址</param>
       /// <param name="title"></param>
       /// <param name="context"></param>
       /// <param name="attachFilePaths"></param>
       /// <param name="serverMailAddress"></param>
       /// <param name="smtppwd"></param>
       /// <param name="smtp"></param>
        /// <param name="Recipients">接收方名称的集合</param>
       /// <param name="tourgroupId"></param>
       /// <param name="tourgroupName"></param>
       /// <param name="Type"></param>
       /// <returns></returns>
        public static bool SendMail(List<string> mailtoAddress, string title, string context, List<string> attachFilePaths, string replyAddress, string recipients, string tourgroupId, string tourgroupName, LogTypeEnum Type)
        {
            //LogEntity log = new LogEntity()
            //{
            //    LogType = Type.ToString(),
            //    Title = title,
            //    TourgroupID = tourgroupId,
            //    TourgroupName = tourgroupName,
            //    Recipients = recipients,//接收人名称
            //    UserName = Thread.CurrentPrincipal.Identity.Name,
            //    RealName = UserInfoCtrl.GetNameByUserName(Thread.CurrentPrincipal.Identity.Name),//UserInfoCtrl.GetNameByUserName(Thread.CurrentPrincipal.Identity.Name),
            //    SendTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
            //    Success = 1,//先置成1，标识发送成功
            //    Descript = "邮件发送成功"
            //};

            System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
            string Smtpserver = SysParameterCtrl.GetValueForKey("SmtpServer");
            string SmtpUsername = SysParameterCtrl.GetValueForKey("SmtpUsername");
            string SmtpPassword = SysParameterCtrl.GetValueForKey("SmtpPassword");

            foreach (string address in mailtoAddress)
            {
                if (!string.IsNullOrEmpty(address) || (address.IndexOf("@") > 0) || (address.Substring(address.IndexOf("@")).IndexOf(".") > 1))//key为mail地址，value为接收方名称
                {
                    msg.To.Add(address);/* msg.To.Add("b@b.com");可以发送给多人 */
                }
            }
            if (string.IsNullOrEmpty(msg.To.ToString()))
            {
                //log.Success = 0;
                //log.Descript = "地址列表有误";
                //LogCtrl.InsertLog(log);
                return false;
            }
            msg.From = new MailAddress(SmtpUsername, "龙润国际", System.Text.Encoding.UTF8);/* 上面3个参数分别是发件人地址，发件人姓名，编码*/
            msg.ReplyTo = new MailAddress(replyAddress, "龙润国际", System.Text.Encoding.UTF8);/* 上面3个参数分别是回复地址，姓名，编码*/
            msg.Subject = title;//邮件标题 
            msg.SubjectEncoding = System.Text.Encoding.UTF8;//邮件标题编码 
            msg.Body = context;
            msg.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码 
            msg.IsBodyHtml = false;//是否是HTML邮件 
            msg.Priority = MailPriority.Normal;//邮件优先级 
            foreach (string filePath in attachFilePaths)
            {
                if (File.Exists(filePath))
                {
                    Attachment data = new Attachment(filePath, MediaTypeNames.Application.Pdf);
                    // Add time stamp information for the file.
                    ContentDisposition disposition = data.ContentDisposition;
                    disposition.CreationDate = System.IO.File.GetCreationTime(filePath);
                    disposition.ModificationDate = System.IO.File.GetLastWriteTime(filePath);
                    disposition.ReadDate = System.IO.File.GetLastAccessTime(filePath);
                    // Add the file attachment to this e-mail message.
                    msg.Attachments.Add(data);
                }
                    //msg.Attachments.Add(new Attachment(filePath));
            }
            
            SmtpClient client = new SmtpClient();

            client.Credentials = new System.Net.NetworkCredential(SmtpUsername, SmtpPassword);
            //上述写你的STMP账号和密码 
            client.Host = Smtpserver;// "STMP服务器";
            object userState = msg;
            try
            {   client.Send(msg);
                
                //释放资源，使得调用处可删除做为附件的临时文件
                msg.Attachments.Dispose(); 

                //写入邮件发送日志
                //LogCtrl.InsertLog(log);
                return true;
            }
            catch (System.Net.Mail.SmtpException ex)
            {
                //log.Success = 0;
                //log.Descript = ex.Message;
                msg.Attachments.Dispose(); 
                //LogCtrl.InsertLog(log);
                return false;
                //throw ex;
            }

        }
    }
}
